﻿namespace Hidistro.SaleSystem.Catalog
{
    using Hidistro.Core;
    using Hidistro.Entities;
    using Hidistro.Entities.Commodities;
    using Hidistro.SqlDal.Commodities;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Globalization;
    using System.Runtime.InteropServices;
    using System.Web.Caching;

    public static class CategoryBrowser
    {
        private const string MainCategoriesCachekey = "DataCache-Categories";

        public static IList<AttributeInfo> GetAttributeInfoByCategoryId(int categoryId, int maxNum = 0x3e8)
        {
            return new AttributeDao().GetAttributeInfoByCategoryId(categoryId, maxNum);
        }

        public static DataTable GetBrandCategories(int categoryId, int maxNum = 0x3e8)
        {
            return new BrandCategoryDao().GetBrandCategories(categoryId, maxNum);
        }
        public static DataSet GetBrand(int categoryId, int maxNum = 0x3e8)
        {
            return new BrandCategoryDao().GetBrand(categoryId, maxNum);
        }

        public static DataSet GerCategorSeo(int categoryId, int maxNum = 0x3e8)
        {
            return new BrandCategoryDao().GerCategorSeo(categoryId);
        }

        public static BrandCategoryInfo GetBrandCategory(int brandId)
        {
            return new BrandCategoryDao().GetBrandCategory(brandId);
        }

        public static DataTable GetCategories()
        {
            DataTable categories = new DataTable();
            categories = HiCache.Get("DataCache-Categories") as DataTable;
            if (categories == null)
            {
                categories = new CategoryDao().GetCategories();
                HiCache.Insert("DataCache-Categories", categories, 360, CacheItemPriority.Normal);
            }
            return categories;
        }

        public static CategoryInfo GetCategory(int categoryId)
        {
            return new CategoryDao().GetCategory(categoryId);
        }
        public static CategoryInfo GetCategoryRewrite(int categoryId)
        {
            return new CategoryDao().GetCategoryRewrite(categoryId);
        }

        public static IList<CategoryInfo> GetMainCategories()
        {
            IList<CategoryInfo> list = new List<CategoryInfo>();
            DataRow[] rowArray = GetCategories().Select("Depth = 1");
            for (int i = 0; i < rowArray.Length; i++)
            {
                list.Add(DataMapper.ConvertDataRowToProductCategory(rowArray[i]));
            }
            return list;
        }

        public static IList<CategoryInfo> GetMaxMainCategories(int maxNum = 0x3e8)
        {
            IList<CategoryInfo> list = new List<CategoryInfo>();
            DataRow[] rowArray = GetCategories().Select("Depth = 1");
            for (int i = 0; (i < maxNum) && (i < rowArray.Length); i++)
            {
                list.Add(DataMapper.ConvertDataRowToProductCategory(rowArray[i]));
            }
            return list;
        }

        public static IList<CategoryInfo> GetMaxSubCategories(int parentCategoryId, int maxNum = 0x3e8)
        {

            IList<CategoryInfo> list = new List<CategoryInfo>();
            DataRow[] rowArray = GetCategories().Select("ParentCategoryId = " + parentCategoryId);
            for (int i = 0; (i < maxNum) && (i < rowArray.Length); i++)
            {
                list.Add(DataMapper.ConvertDataRowToProductCategory(rowArray[i]));
            }
            return list;
        }





        public static IList<CategoryInfo> GetSequenceCategories()
        {
            IList<CategoryInfo> categories = new List<CategoryInfo>();
            IList<CategoryInfo> mainCategories = GetMainCategories();
            foreach (CategoryInfo info in mainCategories)
            {
                categories.Add(info);
                LoadSubCategorys(info.CategoryId, categories);
            }
            return categories;
        }

        public static IList<CategoryInfo> GetSubCategories(int parentCategoryId)
        {
            IList<CategoryInfo> list = new List<CategoryInfo>();
            string filterExpression = "ParentCategoryId = " + parentCategoryId.ToString(CultureInfo.InvariantCulture);
            DataRow[] rowArray = GetCategories().Select(filterExpression);
            for (int i = 0; i < rowArray.Length; i++)
            {
                list.Add(DataMapper.ConvertDataRowToProductCategory(rowArray[i]));
            }
            return list;
        }

        public static DataSet GetThreeLayerCategories()
        {
            return new CategoryDao().GetThreeLayerCategories();
        }

        private static void LoadSubCategorys(int parentCategoryId, IList<CategoryInfo> categories)
        {
            IList<CategoryInfo> subCategories = GetSubCategories(parentCategoryId);
            if ((subCategories != null) && (subCategories.Count > 0))
            {
                foreach (CategoryInfo info in subCategories)
                {
                    categories.Add(info);
                    LoadSubCategorys(info.CategoryId, categories);
                }
            }
        }

        public static CategoryInfo GetCategoryByRewrite(string Rewrite)
        {
            return new CategoryDao().GetCategoryByRewrite(Rewrite);
        }
    }
}

